// ...头文件...
#define ENABLE_MQTT
#define ENABLE_LOG

#include <iostream>
#include <unistd.h>
#include <atomic>
#include <csignal>
#include "lw_log/lw_log.h"

// Only include MQTT header if ENABLE_MQTT is defined
#ifdef ENABLE_MQTT
#include "lw_mqtt/lw_mqtt.h"
#endif

static std::atomic<bool> g_running(true);

void signalHandler(int signal) {
    std::cout << "Received signal " << signal << ", shutting down..." << std::endl;
    g_running = false;
}

// Message callback function
#ifdef ENABLE_MQTT
void onMessage(const std::string& topic, const std::string& payload) {
    LW_LOGI("MQTT", "Received message on topic '%s': %s", topic.c_str(), payload.c_str());
}
#endif

int main() {
    // Initialize log system
    lw_log_init(LW_LOG_DEBUG, stdout, 1, 1);
    LW_LOGI("MAIN", "LW100 MQTT C++ Example");
    
    // Set up signal handlers for graceful shutdown
    signal(SIGINT, signalHandler);
    signal(SIGTERM, signalHandler);

#ifdef ENABLE_MQTT
    // Create MQTT client instance
    LwMqtt mqtt;
    
    // Initialize MQTT client
    LW_LOGI("MAIN", "Initializing MQTT client...");
    if (!mqtt.init("localhost", 1883, "lw100_test_client")) {
        LW_LOGE("MAIN", "Failed to initialize MQTT client");
        return 1;
    }
    
    // Set message callback
    mqtt.setMessageCallback(onMessage);

    // Connect to broker
    LW_LOGI("MAIN", "Connecting to broker...");
    if (!mqtt.connect()) {
        LW_LOGE("MAIN", "Failed to connect to broker");
        if (mqtt.isConnected()) {
            mqtt.disconnect();
        }
        mqtt.deinit();
        return 1;
    }
    
    // Subscribe to a topic
    LW_LOGI("MAIN", "Subscribing to topic 'lw100/test'...");
    if (!mqtt.subscribe("lw100/test", 1)) { // 使用QoS 1
        LW_LOGE("MAIN", "Failed to subscribe to topic");
        if (mqtt.isConnected()) {
            LW_LOGW("MAIN", "MQTT is still connected, disconnecting...");
            mqtt.disconnect();
        }
        mqtt.deinit();
        return 1;
    }
    
    // Publish a message
    LW_LOGI("MAIN", "Publishing message...");
    if (!mqtt.publish("lw100/test", "Hello from LW100!", 1)) { // 使用QoS 1
        LW_LOGE("MAIN", "Failed to publish message");
        mqtt.disconnect();
        mqtt.deinit();
        return 1;
    }
    
    // Process messages for a while
    LW_LOGI("MAIN", "Processing messages for up to 10 seconds...");
    int count = 0;
    while (g_running && count < 10) {
        mqtt.yield(1000); // Process messages for 1 second
        count++;
        LW_LOGI("MAIN", "Processing loop %d/10", count);
    }
    
    // Check if we exited due to signal
    if (!g_running) {
        LW_LOGI("MAIN", "Shutting down due to interrupt signal");
    }
    
    // Disconnect
    LW_LOGI("MAIN", "Disconnecting...");
    mqtt.disconnect();
    
    // Deinitialize
    LW_LOGI("MAIN", "Deinitializing MQTT client...");
    mqtt.deinit();
#else
    LW_LOGI("MAIN", "MQTT support is disabled");
    LW_LOGI("MAIN", "Sleeping for 10 seconds...");
    sleep(10);
#endif
    
    LW_LOGI("MAIN", "MQTT example completed successfully!");
    return 0;
}